package com.kakarote.examine.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.core.utils.UserUtil;
import com.kakarote.examine.entity.BO.SuperExamineFollowBO;
import com.kakarote.examine.entity.PO.ExamineFollow;
import com.kakarote.examine.entity.PO.ExamineRecord;
import com.kakarote.examine.mapper.SuperExamineFollowMapper;
import com.kakarote.examine.service.IExamineRecordService;
import com.kakarote.examine.service.ISuperExamineFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 审批表 服务实现类
 * </p>
 *
 * @author zhangzhiwei
 * @since 2020-11-13
 */
@Service
public class SuperExamineFollowServiceImpl extends BaseServiceImpl<SuperExamineFollowMapper, ExamineFollow> implements ISuperExamineFollowService {

    @Autowired
    private IExamineRecordService examineRecordService;


    /**
     * 切换关注状态
     *
     * @param superExamineFollowBO data
     * @return 是否关注成功
     */
    @Override
    public Boolean changeFollow(SuperExamineFollowBO superExamineFollowBO) {
        if (ObjectUtil.isEmpty(superExamineFollowBO) || ObjectUtil.isEmpty(superExamineFollowBO.getFollowUserId()) || ObjectUtil.isEmpty(superExamineFollowBO.getRecordId())) {
            return Boolean.FALSE;
        }
        List<ExamineFollow> examineFollows = lambdaQuery().eq(ExamineFollow::getRecordId, superExamineFollowBO.getRecordId()).eq(ExamineFollow::getFollowUserId, superExamineFollowBO.getFollowUserId()).list();
        if (CollectionUtil.isEmpty(examineFollows)) {
            ExamineRecord examineRecord = examineRecordService.getById(superExamineFollowBO.getRecordId());
            if (ObjectUtil.isEmpty(examineRecord)) {
                return Boolean.FALSE;
            }
            //添加关注
            ExamineFollow examineFollow = new ExamineFollow();
            examineFollow.setExamineId(examineRecord.getExamineId());
            examineFollow.setFlowId(examineRecord.getFlowId());
            examineFollow.setRecordId(superExamineFollowBO.getRecordId());
            examineFollow.setFollowUserId(superExamineFollowBO.getFollowUserId());
            examineFollow.setBatchId(IdUtil.simpleUUID());
            examineFollow.setCreateUserId(UserUtil.getUserId());
            examineFollow.setCreateTime(LocalDateTime.now());
            save(examineFollow);
        } else {
            //删除关注
            lambdaUpdate().eq(ExamineFollow::getRecordId, superExamineFollowBO.getRecordId()).eq(ExamineFollow::getFollowUserId, superExamineFollowBO.getFollowUserId()).remove();
        }
        return Boolean.TRUE;
    }
}
